package com.dao.impl;

import static com.def.GenericDef.*;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.*;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.dao.IRetPrdInfoDao;
import com.dao.IRetPrdJgeDao;
import com.model.Ret_prd_info;
import com.model.Ret_prd_jge;
import com.model.Ret_prd_jgeId;
import com.rtnBean.DefSumBeen;
import com.rtnBean.DefectInfoBeen;
import com.rtnBean.SimpleRtnBeen;
import com.tx.Xpdefect.DefectInfoBeen2;
import com.tx.Xpdefect.prdOXInfo;
import com.util.Util;

@Repository
public class RetPrdJgeHibernateDao extends
		GenericHibernateDao<Ret_prd_jge, Ret_prd_jgeId> implements
		IRetPrdJgeDao {
	private static final String l_DEF_CODE_STR = "'L001','L002','L006','L014','L015','L016','L017'";
	private static final String B_DEF_CODE = "'Z005','Z006','Z008','Z010','Z011','Z014'";
	private static final List<String> L_DEF_CODE_LIST = Arrays.asList(l_DEF_CODE_STR.replace("'", "").split(","));
	private static final List<String> B_DEF_CODE_LIST = Arrays.asList(B_DEF_CODE.replace("'", "").split(","));
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;

	public Ret_prd_jge getRetPrdJgeTemlate(Ret_prd_info ret_prd_info,
										   String evtSeqId, Integer dataSeqId, Timestamp evtTimestamp) {
		Ret_prd_jge ret_prd_jge = new Ret_prd_jge();
		Ret_prd_jgeId ret_prd_jgeId = new Ret_prd_jgeId();

		ret_prd_jgeId.setPrd_seq_id_fk(ret_prd_info.getPrd_seq_id());
		ret_prd_jgeId.setEvt_seq_id_fk(evtSeqId);
		ret_prd_jgeId.setData_seq_id_fk(dataSeqId);
		ret_prd_jge.setId(ret_prd_jgeId);

		ret_prd_jge.setPath_id_fk(ret_prd_info.getCr_path_id_fk());
		ret_prd_jge.setPath_ver_fk(ret_prd_info.getCr_path_ver_fk());
		ret_prd_jge.setOpe_no_fk(ret_prd_info.getCr_ope_no_fk());
		ret_prd_jge.setOpe_id_fk(ret_prd_info.getCr_ope_id_fk());
		ret_prd_jge.setOpe_ver_fk(ret_prd_info.getCr_ope_ver_fk());
		ret_prd_jge.setProc_id(ret_prd_info.getCr_proc_id_fk());

		ret_prd_jge.setJudge_cnt(0);
		ret_prd_jge.setJudge_tool_id_fk(ret_prd_info.getCr_tool_id_fk());
		ret_prd_jge.setJudge_usr(ret_prd_info.getEvt_usr());
		ret_prd_jge.setJudge_timestamp(evtTimestamp);
		ret_prd_jge.setBox_id_fk(ret_prd_info.getBox_id_fk());
		// ret_prd_jge.setSlot_no_fk(ret_prd_info.getSlot_no());//XppnloutService调用时有bug，在slot
		// no更新前就调用此方法，仍会记录之前的slot
		ret_prd_jge.setPrd_grade_fk(ret_prd_info.getPrd_grade());

		// ret_prd_jge.setFst_judge_tool_id_fk(ret_prd_info.getCr_tool_id_fk());
		ret_prd_jge.setFst_judge_usr(ret_prd_info.getEvt_usr());
		ret_prd_jge.setFst_judge_timestamp(evtTimestamp);

		return ret_prd_jge;
	}

	public SimpleRtnBeen saveRetPrdJgeInfo(Ret_prd_info ret_prd_info,
										   List<DefectInfoBeen> defectInfoList, Timestamp evtTimestamp) {
		int defQty = 0;
		int prd_qry_old = ret_prd_info.getPrd_qty();
		int defectCnt = defectInfoList.size();
		int recordCnt = defectCnt / 5 + ((defectCnt % 5) > 0 ? 1 : 0);
		int defectIndex = 0;
		HashMap<String, Integer> defPosMap = new HashMap<String, Integer>();

		// Ini def position set
		defPosMap = this.getDefPostion(ret_prd_info.getPrd_seq_id());

		for (int dataSeqId = 1; dataSeqId <= recordCnt; dataSeqId++) {
			Ret_prd_jge ret_prd_jge = new Ret_prd_jge();
			ret_prd_jge = this.getRetPrdJgeTemlate(ret_prd_info,
					ret_prd_info.getEvt_seq_id(), dataSeqId, evtTimestamp);

			/***************** 01 *******************/
			// Add defect obj
			ret_prd_jge.setDef_code1(defectInfoList.get(defectIndex)
					.getDefect_code());
			ret_prd_jge.setDef_code1_lc(defectInfoList.get(defectIndex)
					.getPosition());
			ret_prd_jge.setDef_code1_sd(defectInfoList.get(defectIndex)
					.getDefect_side());
			ret_prd_jge.setDef_code1_cnt(defectInfoList.get(defectIndex)
					.getDefect_cnt());

			if (Util.isSpaceCheck(defectInfoList.get(defectIndex).getPosition())) {
				defQty += defectInfoList.get(defectIndex).getDefect_cnt();
			} else {
				if (!defPosMap.containsKey(defectInfoList.get(defectIndex)
						.getPosition())) {
					defPosMap.put(
							defectInfoList.get(defectIndex).getPosition(), 1);
					defQty++;
				}
			}

			defectIndex++;
			if (defectIndex == defectCnt) {
				ret_prd_jge.setJudge_cnt(1);
				this.save(ret_prd_jge);
				break;
			}

			/***************** 02 *******************/
			ret_prd_jge.setDef_code2(defectInfoList.get(defectIndex)
					.getDefect_code());
			ret_prd_jge.setDef_code2_lc(defectInfoList.get(defectIndex)
					.getPosition());
			ret_prd_jge.setDef_code2_sd(defectInfoList.get(defectIndex)
					.getDefect_side());
			ret_prd_jge.setDef_code2_cnt(defectInfoList.get(defectIndex)
					.getDefect_cnt());

			if (Util.isSpaceCheck(defectInfoList.get(defectIndex).getPosition())) {
				defQty += defectInfoList.get(defectIndex).getDefect_cnt();
			} else {
				if (!defPosMap.containsKey(defectInfoList.get(defectIndex)
						.getPosition())) {
					defPosMap.put(
							defectInfoList.get(defectIndex).getPosition(), 1);
					defQty++;
				}
			}

			defectIndex++;
			if (defectIndex == defectCnt) {
				ret_prd_jge.setJudge_cnt(2);
				this.save(ret_prd_jge);
				break;
			}

			/***************** 03 *******************/
			ret_prd_jge.setDef_code3(defectInfoList.get(defectIndex)
					.getDefect_code());
			ret_prd_jge.setDef_code3_lc(defectInfoList.get(defectIndex)
					.getPosition());
			ret_prd_jge.setDef_code3_sd(defectInfoList.get(defectIndex)
					.getDefect_side());
			ret_prd_jge.setDef_code3_cnt(defectInfoList.get(defectIndex)
					.getDefect_cnt());

			if (Util.isSpaceCheck(defectInfoList.get(defectIndex).getPosition())) {
				defQty += defectInfoList.get(defectIndex).getDefect_cnt();
			} else {
				if (!defPosMap.containsKey(defectInfoList.get(defectIndex)
						.getPosition())) {
					defPosMap.put(
							defectInfoList.get(defectIndex).getPosition(), 1);
					defQty++;
				}
			}

			defectIndex++;
			if (defectIndex == defectCnt) {
				ret_prd_jge.setJudge_cnt(3);
				this.save(ret_prd_jge);
				break;
			}

			/***************** 04 *******************/
			ret_prd_jge.setDef_code4(defectInfoList.get(defectIndex)
					.getDefect_code());
			ret_prd_jge.setDef_code4_lc(defectInfoList.get(defectIndex)
					.getPosition());
			ret_prd_jge.setDef_code4_sd(defectInfoList.get(defectIndex)
					.getDefect_side());
			ret_prd_jge.setDef_code4_cnt(defectInfoList.get(defectIndex)
					.getDefect_cnt());

			if (Util.isSpaceCheck(defectInfoList.get(defectIndex).getPosition())) {
				defQty += defectInfoList.get(defectIndex).getDefect_cnt();
			} else {
				if (!defPosMap.containsKey(defectInfoList.get(defectIndex)
						.getPosition())) {
					defPosMap.put(
							defectInfoList.get(defectIndex).getPosition(), 1);
					defQty++;
				}
			}

			defectIndex++;
			if (defectIndex == defectCnt) {
				ret_prd_jge.setJudge_cnt(4);
				this.save(ret_prd_jge);
				break;
			}

			/***************** 05 *******************/
			ret_prd_jge.setDef_code5(defectInfoList.get(defectIndex)
					.getDefect_code());
			ret_prd_jge.setDef_code5_lc(defectInfoList.get(defectIndex)
					.getPosition());
			ret_prd_jge.setDef_code5_sd(defectInfoList.get(defectIndex)
					.getDefect_side());
			ret_prd_jge.setDef_code5_cnt(defectInfoList.get(defectIndex)
					.getDefect_cnt());

			if (Util.isSpaceCheck(defectInfoList.get(defectIndex).getPosition())) {
				defQty += defectInfoList.get(defectIndex).getDefect_cnt();
			} else {
				if (!defPosMap.containsKey(defectInfoList.get(defectIndex)
						.getPosition())) {
					defPosMap.put(
							defectInfoList.get(defectIndex).getPosition(), 1);
					defQty++;
				}
			}

			defectIndex++;
			ret_prd_jge.setJudge_cnt(5);
			this.save(ret_prd_jge);
		}

		ret_prd_info.setPrd_qty(prd_qry_old - defQty);
		// Check prd's prd_qty
		if (0 > ret_prd_info.getPrd_qty()) {
			return new SimpleRtnBeen(PRD_QTY_NOT_ENOUGH, "玻璃 ["
					+ ret_prd_info.getPrd_seq_id() + "]实际数量剩余[" + prd_qry_old
					+ "]不够判定[" + defQty + "]个不良!");

		}

		// Set evt_cate
		ret_prd_info.setEvt_cate(EVT_DEFECT_JUDGE);
		retPrdInfoDao.update(ret_prd_info);

		return new SimpleRtnBeen(ret_prd_info);
	}

	public SimpleRtnBeen delAllDefByPrdId(String prd_seq_id) {
		List<Ret_prd_jge> ret_prd_jgeList = new ArrayList<Ret_prd_jge>();
		String sql = "From Ret_prd_jge WHERE id.prd_seq_id_fk = '" + prd_seq_id
				+ "' ";
		ret_prd_jgeList = this.find(sql);
		if (!ret_prd_jgeList.isEmpty()) {
			this.deleteAll(ret_prd_jgeList);
		}

		return new SimpleRtnBeen(_NORMAL);
	}

	public SimpleRtnBeen queryDefByCondition(String box_id, String prd_seq_id,
											 String path_id_fk, String path_ver_fk, String ope_no_fk,
											 String ope_id_fk, String ope_ver_fk, String proc_id,
											 String begin_timestamp, String end_timestamp) {
		SimpleRtnBeen rtn = new SimpleRtnBeen();
		List<DefectInfoBeen2> defInfoList = new ArrayList<DefectInfoBeen2>();
		List<Ret_prd_jge> ret_prd_jgeList = new ArrayList<Ret_prd_jge>();

		// if (Util.isSpaceCheck(prd_seq_id)) {
		// rtn.setlRc(INVALIED_SPACE_INPUT);
		// rtn.setMsgBuf("查询不良时，玻璃ID不能为空");
		// return rtn;
		// }

		String sql = "";
		if (Util.isSpaceCheck(prd_seq_id)) {
			sql = "From Ret_prd_jge WHERE 1=1 ";
		} else {
			sql = "From Ret_prd_jge WHERE id.prd_seq_id_fk = '" + prd_seq_id
					+ "' ";
		}

		if (!Util.isSpaceCheck(box_id)) {
			List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
			prdInfoList = retPrdInfoDao.getShtListInfo(box_id);
			if (null != prdInfoList) {
				String sht_ids = " AND id.prd_seq_id_fk IN(";
				for (int i = 0; i < prdInfoList.size(); i++) {
					sht_ids += "'" + prdInfoList.get(i).getPrd_seq_id() + "'";
					if (i < prdInfoList.size() - 1) {
						sht_ids += ",";
					}
				}
				sht_ids += ") ";
				sql += sht_ids;
			} else {
				return new SimpleRtnBeen(defInfoList);
			}

		}

		if (!Util.isSpaceCheck(path_id_fk)) {
			sql += " AND path_id_fk = '" + path_id_fk + "' ";
		}

		if (!Util.isSpaceCheck(path_ver_fk)) {
			sql += " AND path_ver_fk = '" + path_ver_fk + "' ";
		}
		if (!Util.isSpaceCheck(ope_no_fk)) {
			sql += " AND ope_no_fk = '" + ope_no_fk + "' ";
		}
		if (!Util.isSpaceCheck(ope_id_fk)) {
			sql += " AND ope_id_fk = '" + ope_id_fk + "' ";
		}
		if (!Util.isSpaceCheck(ope_ver_fk)) {
			sql += " AND ope_ver_fk = '" + ope_ver_fk + "' ";
		}
		if (!Util.isSpaceCheck(proc_id)) {
			sql += " AND proc_id = '" + proc_id + "' ";
		}
		if (!Util.isSpaceCheck(begin_timestamp)) {
			sql += " AND judge_timestamp >= '" + begin_timestamp + "' ";
		}
		if (!Util.isSpaceCheck(end_timestamp)) {
			sql += " AND judge_timestamp <= '" + end_timestamp + "' ";
		}

		ret_prd_jgeList = this.find(sql);
		if (ret_prd_jgeList.isEmpty()) {
			return new SimpleRtnBeen(defInfoList);
		} else {
			for (Ret_prd_jge ret_prd_jge : ret_prd_jgeList) {

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code1())) {
					DefectInfoBeen2 def = new DefectInfoBeen2(ret_prd_jge);
					def.setPrd_seq_id_fk(ret_prd_jge.getId().getPrd_seq_id_fk());
					def.setDefect_code(ret_prd_jge.getDef_code1());
					def.setDefect_cnt(ret_prd_jge.getDef_code1_cnt());
					def.setDefect_side(ret_prd_jge.getDef_code1_sd());
					def.setPosition(ret_prd_jge.getDef_code1_lc());
					if (ret_prd_jge.getRsn_code() != null && !Util.isSpaceCheck(ret_prd_jge.getRsn_code())) {
						def.setOx_show_typ(ret_prd_jge.getRsn_code());
					}
					defInfoList.add(def);
				} else {
					continue;
				}

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code2())) {
					DefectInfoBeen2 def = new DefectInfoBeen2(ret_prd_jge);
					def.setPrd_seq_id_fk(ret_prd_jge.getId().getPrd_seq_id_fk());
					def.setDefect_code(ret_prd_jge.getDef_code2());
					def.setDefect_cnt(ret_prd_jge.getDef_code2_cnt());
					def.setDefect_side(ret_prd_jge.getDef_code2_sd());
					def.setPosition(ret_prd_jge.getDef_code2_lc());

					defInfoList.add(def);
				} else {
					continue;
				}

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code3())) {
					DefectInfoBeen2 def = new DefectInfoBeen2(ret_prd_jge);
					def.setPrd_seq_id_fk(ret_prd_jge.getId().getPrd_seq_id_fk());
					def.setDefect_code(ret_prd_jge.getDef_code3());
					def.setDefect_cnt(ret_prd_jge.getDef_code3_cnt());
					def.setDefect_side(ret_prd_jge.getDef_code3_sd());
					def.setPosition(ret_prd_jge.getDef_code3_lc());

					defInfoList.add(def);
				} else {
					continue;
				}

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code4())) {
					DefectInfoBeen2 def = new DefectInfoBeen2(ret_prd_jge);
					def.setPrd_seq_id_fk(ret_prd_jge.getId().getPrd_seq_id_fk());
					def.setDefect_code(ret_prd_jge.getDef_code4());
					def.setDefect_cnt(ret_prd_jge.getDef_code4_cnt());
					def.setDefect_side(ret_prd_jge.getDef_code4_sd());
					def.setPosition(ret_prd_jge.getDef_code4_lc());

					defInfoList.add(def);
				} else {
					continue;
				}

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code5())) {
					DefectInfoBeen2 def = new DefectInfoBeen2(ret_prd_jge);
					def.setPrd_seq_id_fk(ret_prd_jge.getId().getPrd_seq_id_fk());
					def.setDefect_code(ret_prd_jge.getDef_code5());
					def.setDefect_cnt(ret_prd_jge.getDef_code5_cnt());
					def.setDefect_side(ret_prd_jge.getDef_code5_sd());
					def.setPosition(ret_prd_jge.getDef_code5_lc());

					defInfoList.add(def);
				}


			}

			rtn.setObj(defInfoList);
		}

		return rtn;
	}

	public SimpleRtnBeen queryDefByCondition2(String prd_seq_id, String proc_id,
											  Timestamp timestamp) {
		SimpleRtnBeen rtn = new SimpleRtnBeen();
		List<DefectInfoBeen2> defInfoList = new ArrayList<DefectInfoBeen2>();
		List<Ret_prd_jge> ret_prd_jgeList = new ArrayList<Ret_prd_jge>();

		String sql = "From Ret_prd_jge WHERE prd_seq_id_fk = '" + prd_seq_id
				+ "' AND lst_judge_flg = 'Y'";


		if (!Util.isSpaceCheck(proc_id)) {
			sql += " AND proc_id = '" + proc_id + "' ";
		}

		ret_prd_jgeList = this.find(sql);
		if (ret_prd_jgeList.isEmpty()) {
			return new SimpleRtnBeen(defInfoList);
		} else {
			for (Ret_prd_jge ret_prd_jge : ret_prd_jgeList) {

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code1())) {
					DefectInfoBeen2 def = new DefectInfoBeen2(ret_prd_jge);
					def.setPrd_seq_id_fk(ret_prd_jge.getId().getPrd_seq_id_fk());
					def.setDefect_code(ret_prd_jge.getDef_code1());
					def.setDefect_cnt(ret_prd_jge.getDef_code1_cnt());
					def.setDefect_side(ret_prd_jge.getDef_code1_sd());
					def.setPosition(ret_prd_jge.getDef_code1_lc());
					if (ret_prd_jge.getRsn_code() != null && !Util.isSpaceCheck(ret_prd_jge.getRsn_code())) {
						def.setOx_show_typ(ret_prd_jge.getRsn_code());
					}
					if (ret_prd_jge.getRsn_cate() != null && !Util.isSpaceCheck(ret_prd_jge.getRsn_cate())) {
						if (ret_prd_jge.getRsn_cate().equals("M")) {
							def.setDef_typ("M");
						} else {
							def.setDef_typ("P");
						}
					} else {
						if (ret_prd_jge.getDef_code1().substring(0, 1).equals("L")) {
							def.setDef_typ("M");
						} else {
							def.setDef_typ("P");
						}
					}

					if (timestamp != null) {
						if (Util.getDiffMillisec(timestamp, ret_prd_jge.getJudge_timestamp()) >= 0) {//判断时间大于玻璃logon时间
							def.setShow_flg("Y");
						} else {
							def.setShow_flg("Y");
						}
					} else {
						def.setShow_flg("Y");
					}
					if (!Util.isSpaceCheck(proc_id)) {
						def.setAct_ng_qty(ret_prd_jge.getAct_ng_qty());
					}
					defInfoList.add(def);
				}
			}
			rtn.setObj(defInfoList);
		}
		return rtn;
	}

	public SimpleRtnBeen queryRetPrdJgeByPrd(Ret_prd_info ret_prd_info) {
		List<Ret_prd_jge> ret_prd_jgeList = new ArrayList<Ret_prd_jge>();

		String prd_seq_id = ret_prd_info.getPrd_seq_id();
		String path_id_fk = ret_prd_info.getCr_path_id_fk();
		String path_ver_fk = ret_prd_info.getCr_path_ver_fk();
		String ope_no_fk = ret_prd_info.getCr_ope_no_fk();
		String ope_id_fk = ret_prd_info.getCr_ope_id_fk();
		String ope_ver_fk = ret_prd_info.getCr_ope_ver_fk();
		String proc_id = ret_prd_info.getCr_proc_id_fk();

		if (Util.isSpaceCheck(path_id_fk)) {
			return new SimpleRtnBeen(INVALIED_SPACE_INPUT, "玻璃[" + prd_seq_id
					+ "]的工艺路线[Cr_path_id_fk]不能为空。");
		}
		if (Util.isSpaceCheck(path_ver_fk)) {
			return new SimpleRtnBeen(INVALIED_SPACE_INPUT, "玻璃[" + prd_seq_id
					+ "]的工艺路线版本[Cr_path_ver_fk]不能为空。");
		}
		if (Util.isSpaceCheck(ope_no_fk)) {
			return new SimpleRtnBeen(INVALIED_SPACE_INPUT, "玻璃[" + prd_seq_id
					+ "]的站点编号[Cr_ope_no_fk]不能为空。");
		}
		if (Util.isSpaceCheck(ope_id_fk)) {
			return new SimpleRtnBeen(INVALIED_SPACE_INPUT, "玻璃[" + prd_seq_id
					+ "]的站点ID[Cr_ope_id_fk]不能为空。");
		}
		if (Util.isSpaceCheck(ope_ver_fk)) {
			return new SimpleRtnBeen(INVALIED_SPACE_INPUT, "玻璃[" + prd_seq_id
					+ "]的站点版本[Cr_ope_ver_fk]不能为空。");
		}
		if (Util.isSpaceCheck(proc_id)) {
			return new SimpleRtnBeen(INVALIED_SPACE_INPUT, "玻璃[" + prd_seq_id
					+ "]的制程代码[Cr_proc_id_fk]不能为空。");
		}

		// Find
		String sql = "From Ret_prd_jge WHERE id.prd_seq_id_fk = '"
				+ ret_prd_info.getPrd_seq_id() + "' AND path_id_fk= '"
				+ path_id_fk + "' " + " AND path_ver_fk= '" + path_ver_fk
				+ "' " + " AND ope_no_fk= '" + ope_no_fk + "' "
				+ " AND ope_id_fk= '" + ope_id_fk + "' " + " AND ope_ver_fk= '"
				+ ope_ver_fk + "' " + " AND proc_id= '" + proc_id + "' "
				+ " ORDER BY id.data_seq_id_fk";

		ret_prd_jgeList = this.find(sql);

		return new SimpleRtnBeen(ret_prd_jgeList);
	}

	public static void main(String[] args) {
		List<String> aList = new ArrayList<String>();
		aList.add("a");
		aList.add("b");
		aList.add("a");
		for (String str : aList) {
			System.out.println(str);
		}
	}

	private void setDefLists(String defCode, List<String> outAllDefList, List<String> outLDefList, List<String> outBDefList) {
		if (!StringUtils.isEmpty(defCode)) {
			outAllDefList.add(defCode);
			if (this.L_DEF_CODE_LIST.contains(defCode)) {
				outLDefList.add(defCode);
			} else if (B_DEF_CODE_LIST.contains(defCode)) {
				outBDefList.add(defCode);
			}
		}
	}

	public List<DefSumBeen> summaryDefByBox(String box_id, String cr_ope_id) {
		List<DefSumBeen> defSumBeenList = new ArrayList<DefSumBeen>();


		String allDefs = l_DEF_CODE_STR + "," + B_DEF_CODE;
		StringBuffer hqlBuffer = new StringBuffer("FROM Ret_prd_jge where prd_seq_id_fk in (");
		hqlBuffer.append(" SELECT prd_seq_id FROM Ret_prd_info WHERE BOX_ID_FK = ? )");
		hqlBuffer.append(" AND OPE_ID_FK = ? ");
		hqlBuffer.append(" AND (");
		hqlBuffer.append(" def_code1 in (").append(allDefs).append(")");
		hqlBuffer.append(" OR def_code2 in (").append(allDefs).append(")");
		hqlBuffer.append(" OR def_code3 in (").append(allDefs).append(")");
		hqlBuffer.append(" OR def_code4 in (").append(allDefs).append(")");
		hqlBuffer.append(" OR def_code5 in (").append(allDefs).append(")");
		hqlBuffer.append(" )");


//		List<Integer> allDefList = new ArrayList<Integer>();
//		List<Integer> lDefList = new ArrayList<Integer>();
//		List<Integer> bDefList = new ArrayList<Integer>();
		int allDefCnt, lDefCnt, bDefCnt;
		allDefCnt = lDefCnt = bDefCnt = 0;

//		List<Ret_prd_jge> retPrdJgeList = this.list(hqlBuffer.toString(), "TE000001","JB-01");
		List<Ret_prd_jge> retPrdJgeList = this.list(hqlBuffer.toString(), box_id, cr_ope_id);
		for (Ret_prd_jge ret_prd_jge : retPrdJgeList) {
			String defCode1 = ret_prd_jge.getDef_code1();
			String defCode2 = ret_prd_jge.getDef_code2();
			String defCode3 = ret_prd_jge.getDef_code3();
			String defCode4 = ret_prd_jge.getDef_code4();
			String defCode5 = ret_prd_jge.getDef_code5();
			int defCnt1 = ret_prd_jge.getDef_code1_cnt() == null ? 0 : ret_prd_jge.getDef_code1_cnt();
			int defCnt2 = ret_prd_jge.getDef_code2_cnt() == null ? 0 : ret_prd_jge.getDef_code2_cnt();
			int defCnt3 = ret_prd_jge.getDef_code3_cnt() == null ? 0 : ret_prd_jge.getDef_code3_cnt();
			int defCnt4 = ret_prd_jge.getDef_code4_cnt() == null ? 0 : ret_prd_jge.getDef_code4_cnt();
			int defCnt5 = ret_prd_jge.getDef_code5_cnt() == null ? 0 : ret_prd_jge.getDef_code5_cnt();
			if (!StringUtils.isEmpty(defCode1)) {
				allDefCnt = allDefCnt + defCnt1;
				if (this.L_DEF_CODE_LIST.contains(defCode1)) {
					lDefCnt = lDefCnt + defCnt1;
				} else if (B_DEF_CODE_LIST.contains(defCode1)) {
					bDefCnt = bDefCnt + defCnt1;
				}
			}
			if (!StringUtils.isEmpty(defCode2)) {
				allDefCnt = allDefCnt + defCnt2;
				if (this.L_DEF_CODE_LIST.contains(defCode2)) {
					lDefCnt = lDefCnt + defCnt2;
				} else if (B_DEF_CODE_LIST.contains(defCode2)) {
					bDefCnt = bDefCnt + defCnt2;
				}
			}
			if (!StringUtils.isEmpty(defCode3)) {
				allDefCnt = allDefCnt + defCnt3;
				if (this.L_DEF_CODE_LIST.contains(defCode3)) {
					lDefCnt = lDefCnt + defCnt3;
				} else if (B_DEF_CODE_LIST.contains(defCode3)) {
					bDefCnt = bDefCnt + defCnt3;
				}
			}
			if (!StringUtils.isEmpty(defCode4)) {
				allDefCnt = allDefCnt + defCnt4;
				if (this.L_DEF_CODE_LIST.contains(defCode4)) {
					lDefCnt = lDefCnt + defCnt4;
				} else if (B_DEF_CODE_LIST.contains(defCode4)) {
					bDefCnt = bDefCnt + defCnt4;
				}
			}
			if (!StringUtils.isEmpty(defCode5)) {
				allDefCnt = allDefCnt + defCnt5;
				if (this.L_DEF_CODE_LIST.contains(defCode5)) {
					lDefCnt = lDefCnt + defCnt5;
				} else if (B_DEF_CODE_LIST.contains(defCode5)) {
					bDefCnt = bDefCnt + defCnt5;
				}
			}

//			setDefLists(ret_prd_jge,allDefList,lDefList,bDefList);
		}
		DefSumBeen defSumBeen = new DefSumBeen();
		defSumBeen.setBox_id(box_id);
		defSumBeen.setL_pnl_cnt(lDefCnt);
		defSumBeen.setB_pnl_cnt(bDefCnt);
		defSumBeen.setPnl_cnt(allDefCnt);
		defSumBeen.setK_pnl_cnt(allDefCnt - lDefCnt - bDefCnt);
		defSumBeenList.add(defSumBeen);
//		String queryString = "SELECT SUM(PNL_CNT) AS PNL_CNT,"
//				+ " SUM(CASE WHEN DEF_CODE IN('L001', 'L002', 'L006', 'L014', 'L015', 'L016', 'L017') THEN PNL_CNT ELSE 0 END) AS L_PNL_CNT,"
//				+ " SUM(CASE WHEN DEF_CODE IN('Z005', 'Z006', 'Z008', 'Z010', 'Z011', 'Z014') THEN PNL_CNT ELSE 0 END) AS B_PNL_CNT"
//				+ " FROM ( "
//				+ " 		SELECT PRD_SEQ_ID_FK,DEF_CODE1 DEF_CODE,DEF_CODE1_CNT PNL_CNT,OPE_ID_FK,PROC_ID FROM RET_PRD_JGE WHERE DEF_CODE1 IS NOT NULL AND DEF_CODE1 <>'' UNION ALL "
//				+ " 		SELECT PRD_SEQ_ID_FK,DEF_CODE2 DEF_CODE,DEF_CODE2_CNT PNL_CNT,OPE_ID_FK,PROC_ID FROM RET_PRD_JGE WHERE DEF_CODE2 IS NOT NULL AND DEF_CODE2 <>'' UNION ALL "
//				+ " 		SELECT PRD_SEQ_ID_FK,DEF_CODE3 DEF_CODE,DEF_CODE3_CNT PNL_CNT,OPE_ID_FK,PROC_ID FROM RET_PRD_JGE WHERE DEF_CODE3 IS NOT NULL AND DEF_CODE3 <>'' UNION ALL "
//				+ " 		SELECT PRD_SEQ_ID_FK,DEF_CODE4 DEF_CODE,DEF_CODE4_CNT PNL_CNT,OPE_ID_FK,PROC_ID FROM RET_PRD_JGE WHERE DEF_CODE4 IS NOT NULL AND DEF_CODE4 <>'' UNION ALL "
//				+ " 		SELECT PRD_SEQ_ID_FK,DEF_CODE5 DEF_CODE,DEF_CODE5_CNT PNL_CNT,OPE_ID_FK,PROC_ID FROM RET_PRD_JGE WHERE DEF_CODE5 IS NOT NULL AND DEF_CODE5 <>''"
//				+ " 	) AS A"
//				+ " WHERE (A.DEF_CODE IN ( 'L001', 'L002', 'L006', 'L014', 'L015', 'L016', 'L017', 'Z005', 'Z006', 'Z008', 'Z010', 'Z011', 'Z014') OR A.OPE_ID_FK = '"
//				+ cr_ope_id
//				+ "') "
//				+ " AND PRD_SEQ_ID_FK IN ( SELECT PRD_SEQ_ID FROM RET_PRD_INFO I WHERE I.BOX_ID_FK='"
//				+ box_id + "' )";

//		String queryString = "";
//		List<Object[]> objList = this.findBySQL(queryString);
//		for (int i = 0; i < objList.size(); i++) {
//			DefSumBeen defSumBeen = new DefSumBeen();
//			Object defInfo[] = objList.get(i);
//			defSumBeen.setBox_id(box_id);
//			if (null == defInfo[0]) {
//				defSumBeen.setPnl_cnt(0);
//			} else {
//				defSumBeen.setPnl_cnt(Integer.valueOf(defInfo[0].toString()));
//			}
//			if (null == defInfo[1]) {
//				defSumBeen.setL_pnl_cnt(0);
//			} else {
//				defSumBeen.setL_pnl_cnt(Integer.valueOf(defInfo[1].toString()));
//			}
//			if (null == defInfo[2]) {
//				defSumBeen.setB_pnl_cnt(0);
//			} else {
//				defSumBeen.setB_pnl_cnt(Integer.valueOf(defInfo[2].toString()));
//			}
//			defSumBeen.setK_pnl_cnt(defSumBeen.getPnl_cnt()
//					- defSumBeen.getL_pnl_cnt() - defSumBeen.getB_pnl_cnt());
//
//			defSumBeenList.add(defSumBeen);
//		}


		return defSumBeenList;
	}

	public List<DefectInfoBeen2> prdJge2DefInfoBeen2(
			List<Ret_prd_jge> ret_prd_jgeList) {
		List<DefectInfoBeen2> defInfoList = new ArrayList<DefectInfoBeen2>();

		if (!ret_prd_jgeList.isEmpty()) {
			for (Ret_prd_jge ret_prd_jge : ret_prd_jgeList) {

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code1())) {
					DefectInfoBeen2 def = new DefectInfoBeen2(ret_prd_jge);
					def.setDefect_code(ret_prd_jge.getDef_code1());
					def.setDefect_cnt(ret_prd_jge.getDef_code1_cnt());
					def.setDefect_side(ret_prd_jge.getDef_code1_sd());
					def.setPosition(ret_prd_jge.getDef_code1_lc());

					defInfoList.add(def);
				} else {
					continue;
				}

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code2())) {
					DefectInfoBeen2 def = new DefectInfoBeen2(ret_prd_jge);
					def.setDefect_code(ret_prd_jge.getDef_code2());
					def.setDefect_cnt(ret_prd_jge.getDef_code2_cnt());
					def.setDefect_side(ret_prd_jge.getDef_code2_sd());
					def.setPosition(ret_prd_jge.getDef_code2_lc());

					defInfoList.add(def);
				} else {
					continue;
				}

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code3())) {
					DefectInfoBeen2 def = new DefectInfoBeen2(ret_prd_jge);
					def.setDefect_code(ret_prd_jge.getDef_code3());
					def.setDefect_cnt(ret_prd_jge.getDef_code3_cnt());
					def.setDefect_side(ret_prd_jge.getDef_code3_sd());
					def.setPosition(ret_prd_jge.getDef_code3_lc());

					defInfoList.add(def);
				} else {
					continue;
				}

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code4())) {
					DefectInfoBeen2 def = new DefectInfoBeen2(ret_prd_jge);
					def.setDefect_code(ret_prd_jge.getDef_code4());
					def.setDefect_cnt(ret_prd_jge.getDef_code4_cnt());
					def.setDefect_side(ret_prd_jge.getDef_code4_sd());
					def.setPosition(ret_prd_jge.getDef_code4_lc());

					defInfoList.add(def);
				} else {
					continue;
				}

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code5())) {
					DefectInfoBeen2 def = new DefectInfoBeen2(ret_prd_jge);
					def.setDefect_code(ret_prd_jge.getDef_code5());
					def.setDefect_cnt(ret_prd_jge.getDef_code5_cnt());
					def.setDefect_side(ret_prd_jge.getDef_code5_sd());
					def.setPosition(ret_prd_jge.getDef_code5_lc());

					defInfoList.add(def);
				}
			}
		}

		return defInfoList;
	}

	public List<DefectInfoBeen2> getDefInfoBeen2ByShtIdList(
			List<String> shtIdList) {
		List<DefectInfoBeen2> defInfoList = new ArrayList<DefectInfoBeen2>();
		List<Ret_prd_jge> ret_prd_jgeList = new ArrayList<Ret_prd_jge>();

		if (shtIdList.isEmpty()) {
			return defInfoList;
		}
		String sql = "From Ret_prd_jge WHERE id.prd_seq_id_fk IN(";
		for (String sht_id : shtIdList) {
			sql += "'" + sht_id + "',";
		}
		sql = sql.substring(0, sql.length() - 1);
		sql += ")";
		ret_prd_jgeList = this.find(sql);
		if (ret_prd_jgeList.isEmpty()) {
			return defInfoList;
		} else {
			defInfoList = this.prdJge2DefInfoBeen2(ret_prd_jgeList);
		}

		return defInfoList;
	}

	public HashMap<String, Integer> getDefPostion(String prd_seq_id) {
		SimpleRtnBeen rtn = new SimpleRtnBeen();
		List<Ret_prd_jge> ret_prd_jgeList = new ArrayList<Ret_prd_jge>();
		HashMap<String, Integer> defPosMap = new HashMap<String, Integer>();

		if (Util.isSpaceCheck(prd_seq_id)) {
			return defPosMap;
		}

		String sql = "From Ret_prd_jge WHERE id.prd_seq_id_fk = '" + prd_seq_id
				+ "' ";

		ret_prd_jgeList = this.find(sql);
		if (ret_prd_jgeList.isEmpty()) {
			return defPosMap;
		} else {
			for (Ret_prd_jge ret_prd_jge : ret_prd_jgeList) {
				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code1())) {
					if (!Util.isSpaceCheck(ret_prd_jge.getDef_code1_lc())) {
						if (defPosMap.containsKey(ret_prd_jge.getDef_code1_lc())) {
							defPosMap.put(ret_prd_jge.getDef_code1_lc(), defPosMap.get(ret_prd_jge.getDef_code1_lc()) + 1);
						} else {
							defPosMap.put(ret_prd_jge.getDef_code1_lc(), 1);
						}
					}
				} else {
					continue;
				}

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code2())) {
					if (!Util.isSpaceCheck(ret_prd_jge.getDef_code2_lc())) {
						if (defPosMap
								.containsKey(ret_prd_jge.getDef_code2_lc())) {
							defPosMap
									.put(ret_prd_jge.getDef_code2_lc(),
											defPosMap.get(ret_prd_jge
													.getDef_code2_lc()) + 1);
						} else {
							defPosMap.put(ret_prd_jge.getDef_code2_lc(), 1);
						}
					}
				} else {
					continue;
				}

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code3())) {
					if (!Util.isSpaceCheck(ret_prd_jge.getDef_code3_lc())) {
						if (defPosMap
								.containsKey(ret_prd_jge.getDef_code3_lc())) {
							defPosMap
									.put(ret_prd_jge.getDef_code3_lc(),
											defPosMap.get(ret_prd_jge
													.getDef_code3_lc()) + 1);
						} else {
							defPosMap.put(ret_prd_jge.getDef_code3_lc(), 1);
						}
					}
				} else {
					continue;
				}

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code4())) {
					if (!Util.isSpaceCheck(ret_prd_jge.getDef_code4_lc())) {
						if (defPosMap
								.containsKey(ret_prd_jge.getDef_code4_lc())) {
							defPosMap
									.put(ret_prd_jge.getDef_code4_lc(),
											defPosMap.get(ret_prd_jge
													.getDef_code4_lc()) + 1);
						} else {
							defPosMap.put(ret_prd_jge.getDef_code4_lc(), 1);
						}
					}
				} else {
					continue;
				}

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code5())) {
					if (!Util.isSpaceCheck(ret_prd_jge.getDef_code5_lc())) {
						if (defPosMap
								.containsKey(ret_prd_jge.getDef_code5_lc())) {
							defPosMap
									.put(ret_prd_jge.getDef_code5_lc(),
											defPosMap.get(ret_prd_jge
													.getDef_code5_lc()) + 1);
						} else {
							defPosMap.put(ret_prd_jge.getDef_code5_lc(), 1);
						}
					}
				}
			}

		}

		return defPosMap;
	}

	//defectcode  defectside  defctlc
	public HashMap<String, Integer> getDefPostion2(String prd_seq_id) {
		SimpleRtnBeen rtn = new SimpleRtnBeen();
		List<Ret_prd_jge> ret_prd_jgeList = new ArrayList<Ret_prd_jge>();
		HashMap<String, Integer> defPosMap = new HashMap<String, Integer>();

		if (Util.isSpaceCheck(prd_seq_id)) {
			return defPosMap;
		}

		String sql = "From Ret_prd_jge WHERE id.prd_seq_id_fk = '" + prd_seq_id
				+ "' ";

		ret_prd_jgeList = this.find(sql);
		if (ret_prd_jgeList.isEmpty()) {
			return defPosMap;
		} else {
			for (Ret_prd_jge ret_prd_jge : ret_prd_jgeList) {
				//code + side + localcation
				String defectPo = ret_prd_jge.getDef_code1()+"_" + ret_prd_jge.getDef_code1_sd() + "_" + ret_prd_jge.getDef_code1_lc();

				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code1())) {
					if (!Util.isSpaceCheck(ret_prd_jge.getDef_code1_lc())) {
						if(!Util.isSpaceCheck(ret_prd_jge.getDef_code1_sd())){
							if (defPosMap.containsKey(defectPo)) {
								defPosMap.put(defectPo, defPosMap.get(defectPo) + ret_prd_jge.getDef_code1_cnt());
							} else {
								defPosMap.put(defectPo, ret_prd_jge.getDef_code1_cnt());
							}
						}
					}
				} else {
					continue;
				}

			}

		}

		return defPosMap;
	}

	/**
	 * add prd jge only defe_code1 & update prd info & update RET_PRD_GRD_INFO
	 */
	public SimpleRtnBeen saveRetPrdJgeInfo_new(Ret_prd_info ret_prd_info,
											   List<DefectInfoBeen> defectInfoList, Timestamp evtTimestamp,
											   String prd_grd) {

		int ng_qty = 0;
		int proc_ng_qty = 0;
		int cus_ng_qty = 0;
		int act_ng_qty = 0;
		int act_cus_ng_qty = 0;
		int act_proc_ng_qty = 0;

		HashMap<String, Integer> defPosMap = new HashMap<String, Integer>();
		int defectIndex = 0;
		List<Ret_prd_jge> prdJgeList = new ArrayList<Ret_prd_jge>();

		for (int dataSeqId = 1; dataSeqId <= defectInfoList.size(); dataSeqId++) {
			Ret_prd_jge ret_prd_jge = new Ret_prd_jge();
			ret_prd_jge = this.getRetPrdJgeTemlate(ret_prd_info,
					ret_prd_info.getEvt_seq_id(), dataSeqId, evtTimestamp);

			// Add defect obj
			ret_prd_jge.setDef_code1(defectInfoList.get(defectIndex).getDefect_code());
			ret_prd_jge.setDef_code1_lc(defectInfoList.get(defectIndex).getPosition());
			ret_prd_jge.setDef_code1_sd(defectInfoList.get(defectIndex).getDefect_side());
			ret_prd_jge.setDef_code1_cnt(defectInfoList.get(defectIndex).getDefect_cnt());
			if (defectInfoList.get(defectIndex).getOx_show_typ() != null && !Util.isSpaceCheck(defectInfoList.get(defectIndex).getOx_show_typ())) {
				ret_prd_jge.setRsn_code(defectInfoList.get(defectIndex).getOx_show_typ());
			}
			ret_prd_jge.setRsn_cate(defectInfoList.get(defectIndex).getDef_typ());
			ret_prd_jge.setPrd_grade_fk(prd_grd);
			//统计当前判定的def的有效判定
			if (defectInfoList.get(defectIndex).getDefect_cnt() > 0) {
				if (!defPosMap.containsKey(defectInfoList.get(defectIndex)
						.getPosition())) {//未有相同位子的判定
					defPosMap.put(
							defectInfoList.get(defectIndex).getPosition(), 1);
					act_ng_qty++;
					if (defectInfoList.get(defectIndex).getDef_typ().equals("P")) {
						act_proc_ng_qty++;
					} else if (defectInfoList.get(defectIndex).getDef_typ().equals("M")) {
						act_cus_ng_qty++;
					}
				}
				ng_qty++;
			}

			if (defectInfoList.get(defectIndex).getDef_typ().equals("P") &&
					defectInfoList.get(defectIndex).getDefect_cnt() > 0) {
				proc_ng_qty++;
			} else if (defectInfoList.get(defectIndex).getDef_typ().equals("M") &&
					defectInfoList.get(defectIndex).getDefect_cnt() > 0) {
				cus_ng_qty++;
			}
			defectIndex++;
			ret_prd_jge.setJudge_cnt(1);
			ret_prd_jge.setLst_judge_flg("Y");
			prdJgeList.add(ret_prd_jge);
		}
		for (int i = 0; i < prdJgeList.size(); i++) {
			prdJgeList.get(i).setNg_qty(ng_qty);
			prdJgeList.get(i).setCus_ng_qty(cus_ng_qty);
			prdJgeList.get(i).setProc_ng_qty(proc_ng_qty);
			prdJgeList.get(i).setAct_ng_qty(act_ng_qty);
			prdJgeList.get(i).setAct_cus_ng_qty(act_cus_ng_qty);
			prdJgeList.get(i).setAct_proc_ng_qty(act_proc_ng_qty);
		}
		this.saveOrUpdateAll(prdJgeList);

		return new SimpleRtnBeen(prdJgeList);
	}

	public SimpleRtnBeen queryRetPrdJgeByPrd_new(Ret_prd_info ret_prd_info) {
		List<Ret_prd_jge> ret_prd_jgeList = new ArrayList<Ret_prd_jge>();

		String prd_seq_id = ret_prd_info.getPrd_seq_id();
		String path_id_fk = ret_prd_info.getCr_path_id_fk();
		String path_ver_fk = ret_prd_info.getCr_path_ver_fk();
		String ope_no_fk = ret_prd_info.getCr_ope_no_fk();
		String ope_id_fk = ret_prd_info.getCr_ope_id_fk();
		String ope_ver_fk = ret_prd_info.getCr_ope_ver_fk();
		String proc_id = ret_prd_info.getCr_proc_id_fk();

		if (Util.isSpaceCheck(path_id_fk)) {
			return new SimpleRtnBeen(INVALIED_SPACE_INPUT, "玻璃[" + prd_seq_id
					+ "]的工艺路线[Cr_path_id_fk]不能为空。");
		}
		if (Util.isSpaceCheck(path_ver_fk)) {
			return new SimpleRtnBeen(INVALIED_SPACE_INPUT, "玻璃[" + prd_seq_id
					+ "]的工艺路线版本[Cr_path_ver_fk]不能为空。");
		}
		if (Util.isSpaceCheck(ope_no_fk)) {
			return new SimpleRtnBeen(INVALIED_SPACE_INPUT, "玻璃[" + prd_seq_id
					+ "]的站点编号[Cr_ope_no_fk]不能为空。");
		}
		if (Util.isSpaceCheck(ope_id_fk)) {
			return new SimpleRtnBeen(INVALIED_SPACE_INPUT, "玻璃[" + prd_seq_id
					+ "]的站点ID[Cr_ope_id_fk]不能为空。");
		}
		if (Util.isSpaceCheck(ope_ver_fk)) {
			return new SimpleRtnBeen(INVALIED_SPACE_INPUT, "玻璃[" + prd_seq_id
					+ "]的站点版本[Cr_ope_ver_fk]不能为空。");
		}
		if (Util.isSpaceCheck(proc_id)) {
			return new SimpleRtnBeen(INVALIED_SPACE_INPUT, "玻璃[" + prd_seq_id
					+ "]的制程代码[Cr_proc_id_fk]不能为空。");
		}

		// Find
//		String sql = "From Ret_prd_jge WHERE id.prd_seq_id_fk = '"
//				+ ret_prd_info.getPrd_seq_id() + "' AND path_id_fk= '"
//				+ path_id_fk + "' " + " AND path_ver_fk= '" + path_ver_fk
//				+ "' " + " AND ope_no_fk= '" + ope_no_fk + "' "
//				+ " AND ope_id_fk= '" + ope_id_fk + "' " + " AND ope_ver_fk= '"
//				+ ope_ver_fk + "' " + " AND proc_id= '" + proc_id + "' AND lst_judge_flg = 'Y'"
//				+ " ORDER BY id.data_seq_id_fk";

		String sql = "From Ret_prd_jge WHERE id.prd_seq_id_fk ='"
				+ ret_prd_info.getPrd_seq_id() + "' AND lst_judge_flg = 'Y'"
				+ " ORDER BY id.data_seq_id_fk";

		ret_prd_jgeList = this.find(sql);

		return new SimpleRtnBeen(ret_prd_jgeList);
	}

	//查询DEFECT cnt不是0的defect
	//defectcode   defectside   defctlc
	public SimpleRtnBeen queryNotZero(String prd_seq_id) {
		SimpleRtnBeen rtn = new SimpleRtnBeen();
		//String --- >  code + side + localcation
		HashMap<String, Integer> defPostion = getDefPostion2(prd_seq_id);
		//获得不为0的判定位置的位置
		List<String> notZeroPostion = new ArrayList<>();
		Iterator iter = defPostion.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			Object key = entry.getKey();
			Object val = entry.getValue();

			if (val instanceof Integer) {
				Integer defCnt = Integer.parseInt(val.toString());
				if (!defCnt.equals(0)) {
					notZeroPostion.add(key.toString());
				}
			}
		}
		rtn.setObj(notZeroPostion);
		return rtn;
	}


	public List<Ret_prd_jge> queryALLNotZeroDef(String prd_seq_id){
		String hql = "From Ret_prd_jge WHERE id.prd_seq_id_fk = '" + prd_seq_id
				+ "' ";
		List<Ret_prd_jge> retPrdJgeList = this.find(hql);

		HashMap<String,Integer> postionCnt = new HashMap<>();
		if(retPrdJgeList.isEmpty()){
			return null;
		}else{
			for(int i = 0; i < retPrdJgeList.size(); i++){
				String defCode = retPrdJgeList.get(i).getDef_code1();
				String defSide = retPrdJgeList.get(i).getDef_code1_sd();
				String defLoc = retPrdJgeList.get(i).getDef_code1_lc();
				String defCodeSideLoc = defCode+"_"+defSide+"_"+defLoc;
				if (!Util.isSpaceCheck(defCode)) {
					if (!Util.isSpaceCheck(defSide)) {
						if(!Util.isSpaceCheck(defLoc)){
							if (postionCnt.containsKey(defCodeSideLoc)) {
								postionCnt.put(defCodeSideLoc, postionCnt.get(defCodeSideLoc) + retPrdJgeList.get(i).getDef_code1_cnt());
							} else {
								postionCnt.put(defCodeSideLoc, retPrdJgeList.get(i).getDef_code1_cnt());
							}
						}
					}
				} else {
					continue;
				}
			}
		}

		List<String> notZeroPostion = new ArrayList<>();
		Iterator iter = postionCnt.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			Object key = entry.getKey();
			Object val = entry.getValue();

			if (val instanceof Integer) {
				Integer defCnt = Integer.parseInt(val.toString());
				if (!defCnt.equals(0)) {
					notZeroPostion.add(key.toString());
				}
			}
		}

		//筛选出不是0的defctCNt的最后一笔记录
		HashMap<String,Ret_prd_jge> onlyOneJge= new HashMap<>();
		if(!notZeroPostion.isEmpty()){
			HashMap<String,Ret_prd_jge> ret_prd_jgeHashMap1 = new HashMap<>();
			for(int j = 0 ;j < retPrdJgeList.size();j++){
				String defCode = retPrdJgeList.get(j).getDef_code1();
				String defSide = retPrdJgeList.get(j).getDef_code1_sd();
				String defLoc = retPrdJgeList.get(j).getDef_code1_lc();
				String defCodeSideLoc = defCode+"_"+defSide+"_"+defLoc;
				if(notZeroPostion.contains(defCodeSideLoc)){
					if (onlyOneJge.containsKey(defCodeSideLoc)) {
						if (Util.getDiffMillisec(onlyOneJge.get(defCodeSideLoc).getJudge_timestamp(), retPrdJgeList.get(j).getJudge_timestamp()) > 0) {
							onlyOneJge.put(defCodeSideLoc, retPrdJgeList.get(j));
						}
					} else {
						onlyOneJge.put(defCodeSideLoc, retPrdJgeList.get(j));
					}
				}
			}
		}

		List<Ret_prd_jge> retPrdJgeList2 = new ArrayList<>();
		Iterator iter2 = onlyOneJge.entrySet().iterator();        //获取key和value的set
		while (iter2.hasNext()) {
			Map.Entry entry = (Map.Entry) iter2.next();        //把hashmap转成Iterator再迭代到entry
			Object key = entry.getKey();        //从entry获取key
			Object val = entry.getValue();    //从entry获取value
			retPrdJgeList2.add((Ret_prd_jge) val);
		}

		return retPrdJgeList2;

	}


	//返回ret_prd_jge中def_nt不为0的所有defect
	public SimpleRtnBeen queryRetPrdJgeByPrd_new2(String prd_seq_id, String proc_id) {
		SimpleRtnBeen rtn = new SimpleRtnBeen();
		List<Ret_prd_jge> retPrdJgeList = new ArrayList<>();
		retPrdJgeList = queryALLNotZeroDef(prd_seq_id);

		List<DefectInfoBeen2> defInfoList = new ArrayList<DefectInfoBeen2>();
		if (retPrdJgeList == null || retPrdJgeList.isEmpty()) {
			return new SimpleRtnBeen(defInfoList);
		} else {
			for (Ret_prd_jge ret_prd_jge : retPrdJgeList) {
				if (!Util.isSpaceCheck(ret_prd_jge.getDef_code1())) {
					DefectInfoBeen2 def = new DefectInfoBeen2(ret_prd_jge);
					def.setPrd_seq_id_fk(ret_prd_jge.getId().getPrd_seq_id_fk());
					def.setDefect_code(ret_prd_jge.getDef_code1());
					def.setDefect_cnt(ret_prd_jge.getDef_code1_cnt());
					def.setDefect_side(ret_prd_jge.getDef_code1_sd());
					def.setPosition(ret_prd_jge.getDef_code1_lc());
					if (ret_prd_jge.getRsn_code() != null && !Util.isSpaceCheck(ret_prd_jge.getRsn_code())) {
						def.setOx_show_typ(ret_prd_jge.getRsn_code());
					}
					if (ret_prd_jge.getRsn_cate() != null && !Util.isSpaceCheck(ret_prd_jge.getRsn_cate())) {
						if (ret_prd_jge.getRsn_cate().equals("M")) {
							def.setDef_typ("M");
						} else {
							def.setDef_typ("P");
						}
					} else {
						if (ret_prd_jge.getDef_code1().substring(0, 1).equals("L")) {
							def.setDef_typ("M");
						} else {
							def.setDef_typ("P");
						}
					}
					def.setShow_flg("Y");
					if (!Util.isSpaceCheck(proc_id)) {
						def.setAct_ng_qty(ret_prd_jge.getAct_ng_qty());
					}
					defInfoList.add(def);
				}
			}
			rtn.setObj(defInfoList);
		}
		return rtn;
	}

	public prdOXInfo getOXInfo(Ret_prd_info ret_prd_info, boolean hxFlg) {
		List<Ret_prd_jge> ret_prd_jgeList = new ArrayList<Ret_prd_jge>();
		HashMap<String, Integer> defPosMap = new HashMap<String, Integer>();
		prdOXInfo prd_OX_info = new prdOXInfo();
		char[] Digit = {'O', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
				'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
		Integer prd_qty = 0;
		Integer cus_ng_qty = 0;
		Integer proc_ng_qty = 0;
		Integer act_cus_ng_qty = 0;
		Integer act_proc_ng_qty = 0;
		String prd_seq_grd = "";
		String prd_seq_grd_t = "";
		String prd_seq_grd_c = "";
		String grd_sd;
		Integer grd_t;
		Integer grd_c;

		String sql = "From Ret_prd_jge WHERE id.prd_seq_id_fk = '" + ret_prd_info.getPrd_seq_id()
				+ "' AND lst_judge_flg = 'Y' AND proc_id <> 'JBYZ' order by def_code1_lc, JUDGE_TIMESTAMP";
		ret_prd_jgeList = this.find(sql);
		if (ret_prd_jgeList.isEmpty()) {
			for (int i = 0; i < ret_prd_info.getPrd_std_qty(); i++) {
				prd_seq_grd = prd_seq_grd + "O";
				prd_seq_grd_t = prd_seq_grd_t + "O";
				prd_seq_grd_c = prd_seq_grd_c + "O";
			}
			prd_qty = ret_prd_info.getPrd_std_qty();
		} else {
			for (Ret_prd_jge ret_prd_jge : ret_prd_jgeList) {
				//统计有效判定
				if (defPosMap.containsKey(ret_prd_jge.getDef_code1_lc())) {
					defPosMap.put(ret_prd_jge.getDef_code1_lc(),
							defPosMap.get(ret_prd_jge.getDef_code1_lc()) + ret_prd_jge.getDef_code1_cnt());
				} else {
					defPosMap.put(
							ret_prd_jge.getDef_code1_lc(), ret_prd_jge.getDef_code1_cnt());
				}
				if (ret_prd_jge.getRsn_cate() != null && !Util.isSpaceCheck(ret_prd_jge.getRsn_cate())) {
					if (ret_prd_jge.getRsn_cate().equals("M")) {
						cus_ng_qty = cus_ng_qty + ret_prd_jge.getDef_code1_cnt();
					} else {
						proc_ng_qty = proc_ng_qty + ret_prd_jge.getDef_code1_cnt();
					}
				} else {
					if (ret_prd_jge.getDef_code1().substring(0, 1).equals("L")) {//来料不良
						cus_ng_qty = cus_ng_qty + ret_prd_jge.getDef_code1_cnt();
					} else {//制程不良
						proc_ng_qty = proc_ng_qty + ret_prd_jge.getDef_code1_cnt();
					}
				}
			}

			for (Ret_prd_jge ret_prd_jge : ret_prd_jgeList) {
				System.out.println(ret_prd_jge.getDef_code1_lc() + "|" + defPosMap.get(ret_prd_jge.getDef_code1_lc()));
				if (defPosMap.get(ret_prd_jge.getDef_code1_lc()) > 0 && ret_prd_jge.getDef_code1_cnt() > 0) {
					if (ret_prd_jge.getRsn_cate() != null && !Util.isSpaceCheck(ret_prd_jge.getRsn_cate())) {
						if (ret_prd_jge.getRsn_cate().equals("M")) {
							act_cus_ng_qty = act_cus_ng_qty + ret_prd_jge.getDef_code1_cnt();
						} else {
							act_proc_ng_qty = act_proc_ng_qty + ret_prd_jge.getDef_code1_cnt();
						}
					} else {
						if (ret_prd_jge.getDef_code1().substring(0, 1).equals("L")) {
							act_cus_ng_qty = act_cus_ng_qty + ret_prd_jge.getDef_code1_cnt();
						} else {
							act_proc_ng_qty = act_proc_ng_qty + ret_prd_jge.getDef_code1_cnt();
						}
					}
					defPosMap.put(ret_prd_jge.getDef_code1_lc(), 0);
				}
			}
			/**
			 * set prd_seq_grd,prd_seq_grd_t,prd_seq_grd_c
			 */
			for (int i = 1; i <= ret_prd_info.getPrd_std_qty(); i++) {
				grd_sd = "O";
				grd_t = 0;
				grd_c = 0;
				for (int j = 0; j < ret_prd_jgeList.size(); j++) {
					final Ret_prd_jge ret_prd_jge = ret_prd_jgeList.get(j);
					String[] aa = ret_prd_jge.getDef_code1_lc().split("\\*");
					Integer x = Integer.valueOf(aa[0]);
					Integer y = Integer.valueOf(aa[1]);
					//公式有所变化 (x-1)*y_axis_cnt + y
//					Integer strNo = (x-1)*ret_prd_info.getX_axis_cnt_fk() + y;
					Integer strNo = hxFlg ? (x - 1) * ret_prd_info.getY_axis_cnt_fk() + y : (x - 1) * ret_prd_info.getX_axis_cnt_fk() + y;
					if (i == strNo) {

						if (ret_prd_jge.getDef_code1_sd().equals("T")) {
							grd_t = grd_t + ret_prd_jge.getDef_code1_cnt();
							if (grd_t > 0) {
								grd_sd = ret_prd_jge.getDef_code1();
							} else {
								grd_sd = "O";
							}
						} else if (ret_prd_jge.getDef_code1_sd().equals("C")) {
							grd_c = grd_c + ret_prd_jge.getDef_code1_cnt();
							if (grd_c > 0) {
								grd_sd = ret_prd_jge.getDef_code1();
							} else {
								grd_sd = "O";
							}
						}
						//当rsn_code不等于空时，1700保留时
//						if (!StringUtils.isEmpty(ret_prd_jge.getRsn_code())){
//							if(ret_prd_jge.getDef_code1_sd().equals("T")){
//								grd_t = grd_t + ret_prd_jge.getDef_code1_cnt();
//								if(grd_t > 0){
//									grd_sd = ret_prd_jge.getRsn_code();
//								}else{
//									grd_sd = "O";
//								}
//							}else if(ret_prd_jge.getDef_code1_sd().equals("C")){
//								grd_c = grd_c + ret_prd_jge.getDef_code1_cnt();
//								if(grd_c > 0){
//									grd_sd = ret_prd_jge.getRsn_code();
//								}else{
//									grd_sd = "O";
//								}
//							}
//						}else{
//							//当rsn_code为空时，1400判定
//							if(grd_sd.equals("O")){
//								if(ret_prd_jge.getDef_code1_sd().equals("T")){
//									grd_t = grd_t + ret_prd_jge.getDef_code1_cnt();
//									if(grd_t > 0){
//										grd_sd = "1";
//									}else{
//										grd_sd = "O";
//									}
//								}else if(ret_prd_jge.getDef_code1_sd().equals("C")){
//									grd_c = grd_c + ret_prd_jge.getDef_code1_cnt();
//									if(grd_c > 0){
//										grd_sd = "2";
//									}else{
//										grd_sd = "O";
//									}
//								}
//							}else if(grd_sd.equals("1")){
//								if(ret_prd_jge.getDef_code1_sd().equals("T")){
//									grd_t = grd_t + ret_prd_jge.getDef_code1_cnt();
//									if(grd_t > 0){
//										grd_sd = "1";
//									}else{
//										grd_sd = "O";
//									}
//								}else if(ret_prd_jge.getDef_code1_sd().equals("C")){
//									grd_c = grd_c + ret_prd_jge.getDef_code1_cnt();
//									if(grd_c > 0){
//										grd_sd = "3";
//									}else{
//										grd_sd = "1";
//									}
//								}
//
//							}else if(grd_sd.equals("2")){
//								 if(ret_prd_jge.getDef_code1_sd().equals("T")){
//									grd_t = grd_t + ret_prd_jge.getDef_code1_cnt();
//									if(grd_t > 0){
//										grd_sd = "3";
//									}else{
//										grd_sd = "2";
//									}
//								}else if(ret_prd_jge.getDef_code1_sd().equals("C")){
//									grd_c = grd_c + ret_prd_jge.getDef_code1_cnt();
//									if(grd_c > 0){
//										grd_sd = "2";
//									}else{
//										grd_sd = "O";
//									}
//								}
//							}
//						}
					}
				}
				prd_seq_grd = prd_seq_grd + grd_sd;
				prd_seq_grd_t = prd_seq_grd_t + Digit[grd_t];
				prd_seq_grd_c = prd_seq_grd_c + Digit[grd_c];
			}

			prd_qty = ret_prd_info.getPrd_std_qty() - act_cus_ng_qty - act_proc_ng_qty;
		}

		prd_OX_info.setPrd_qty(prd_qty);
		prd_OX_info.setPrd_seq_grd(prd_seq_grd);
		prd_OX_info.setPrd_seq_grd_t(prd_seq_grd_t);
		prd_OX_info.setPrd_seq_grd_c(prd_seq_grd_c);
		prd_OX_info.setCus_ng_qty(cus_ng_qty);
		prd_OX_info.setProc_ng_qty(proc_ng_qty);
		prd_OX_info.setAct_cus_ng_qty(act_cus_ng_qty);
		prd_OX_info.setAct_proc_ng_qty(act_proc_ng_qty);

		return prd_OX_info;
	}
}
